﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private Double h, w;
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            String rep=".";
            String [] fichiers;
            fichiers = new string[] { };
            if (textBox1.Text != "")
                rep = textBox1.Text;
            GetFiles(ref fichiers, rep);
            listBox1.Items.Clear();
            for (int i = 0; i < fichiers.Length; i++)
            {
                listBox1.Items.Add(fichiers[i]);
            }

        }
        private void GetFiles(ref string[] files, string chemin)
        {
            //string souschemin;
            string[] fichiers;
            //## chemin et correspondant à la structure file.
            //## Il correspond au tableau files mais pour le 
            //## répertoire en cours de traitement.
            int i;
            //## cours de traitement.
            int dimens = 0;
            //## redimensionné. "Images|*.jpg;*.png;*.bmp;*.gif|All Files|*.*"
            try
            {
                fichiers = System.IO.Directory.GetFileSystemEntries(chemin);
                if (fichiers.Length > 0)
                {
                    if (files == null)
                    {
                        // ERROR: Not supported in C#: ReDimStatement

                    }
                    else
                    {
                        dimens = files.Length;

                    }
                    int j = 0;
                    for (i = 0; i <= fichiers.Length - 1; i++)
                    {
                        if (fichiers[i].EndsWith(".jpg") || fichiers[i].EndsWith(".png") || fichiers[i].EndsWith(".bmp") || fichiers[i].EndsWith(".gif"))
                        {
                            if (files == null)
                                files = new string[] { };
                            Array.Resize(ref files, files.Length + 1);
                            files[dimens + j] = fichiers[i];
                            j = j + 1;
                        }
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            image1.Source = new BitmapImage(new Uri(listBox1.SelectedItem.ToString()));
           // image1.HorizontalAlignment = HorizontalAlignment.Left;
           // image1.VerticalAlignment = VerticalAlignment.Top;
            h = image1.Height;
            w = image1.Width;
        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (image1.Source!= null)
            {
                image1.Height = h * slider1.Value;
                image1.Width = w * slider1.Value;
            }
        }
   

    }
}
